﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LightCube.Transform
{
    public class TranslationTransform : ITransform
    {
        private Point3D offset;
        private bool wrap;
        
        public TranslationTransform(Point3D offset, bool wrap)
        {
            this.offset = offset;
            this.wrap = wrap;
        }

        public void Transform(Buffer3D buffer)
        {
            var tempBuffer = buffer.CopyNew();
            
            Point3D pixel = Point3D.Zero;
            for (; pixel.Z < buffer.Size; pixel.Z++)
            {
                for (; pixel.Y < buffer.Size; pixel.Y++)
                {
                    for (; pixel.X < buffer.Size; pixel.X++)
                    {
                        var targetPixel = pixel.Offset(offset);

                        if (targetPixel.OutOfBound(buffer.Size))
                        {
                            if (wrap)
                                targetPixel = targetPixel.Wrap(buffer.Size);
                            else
                                continue;
                        }

                        buffer.SetPixel(targetPixel, tempBuffer.GetPixcel(pixel));
                    }
                }
            }
        }
    }
}
